package com.EJ.ejcompanyweb.HttpURLC;

import android.content.Context;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.EJ.ejcompanyweb.Util.ADFilterTool;

public class NoAdWebViewClient extends WebViewClient {
    private static NoAdWebViewClient noAdWebViewClient;
    private  String homeurl;
    private Context context;
    private String StartUrl;

    public NoAdWebViewClient(String homeurl, Context context) {
        this.homeurl = homeurl;
        this.context = context;
    }
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        homeurl=url;
        view.loadUrl(url);
        return true;
    }
    @Override
    public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
        url = url.toLowerCase();
        if(!url.contains(homeurl)){
            if (ADFilterTool.hasAd(context, url)) {
                return super.shouldInterceptRequest(view, url);//正常加载
            }else{
                return new WebResourceResponse(null,null,null);//含有广告资源屏蔽请求
            }
        }else{
            return super.shouldInterceptRequest(view, url);
        }


    }
    public static NoAdWebViewClient getInstance(String homeurl, Context context){
        if (noAdWebViewClient==null){
            noAdWebViewClient=new NoAdWebViewClient(homeurl,context);
        }else {
            noAdWebViewClient.homeurl= homeurl;
            noAdWebViewClient.context=context;
        }
        return noAdWebViewClient;
    }
}
